<?php
session_start();

require_once '../classes/database.php';

Database::getDBOInstance();
$email = $_SESSION['email'];

if (!$email) {
	include 'login.php';
}

$user_name = "Test User";
//$email = $_SESSION['email'];
$email = 'a@pdn.ac.lk';

include 'admin/config.php';

$connetion = mysql_connect($db_host, $db_username, $db_password) or die('Error in DB');
$db = mysql_select_db($db_name) or die('Érror in selecting db');

//Variable to hold values to display
$array_code = array(); 
$array_name = array();
$array_day = array();
$array_start = array();
$array_end = array();

$html_table_body = "";
$html_statistics = "";
$html_statistics_count = 0;
$html_statistics_start = "";
$html_statistics_end = "";

$action = $_GET['action'];

switch ($action){
	case 'drop': 
		drop_subject($_GET['code']);
		break;
	case 'finalize':
		finalize();
		 break;
}

get_allocations();
generate_table_view();
generate_statistics_view();

function generate_table_view(){
	//echo "here";
	global $email, $array_code, $array_name, $array_day, $array_start, $array_end, $html_table_body;
	
	$html = "";
	for ($i = 0; $i < count($array_code); $i++){
		$html = $html . 
			"<tr>
				<td>" . $array_code[$i] . "&nbsp;</td>
				<td>" . $array_name[$i] . "&nbsp;</td>
				<td align=\"center\">" . $array_day[$i] . "&nbsp;</td>
				<td align=\"center\">" . $array_start[$i] . "&nbsp;</td>
				<td align=\"center\">" . $array_end[$i] . "&nbsp;</td>
				<td align=\"center\"><a href='index.php?action=drop&code=".$array_code[$i]."'>Drop</a></td>
			</tr>"; 
	}
	

	$html_table_body = $html;
	
}


function generate_statistics_view(){
	global $html_statistics, $html_statistics_count, $email;
	
	//current allocations
	$query = "select * from Supervises_Exam where email = '$email'";
	$result = mysql_query($query);
	
	$html_statistics_count = mysql_num_rows($result);
}

function drop_subject($sub_code){
	global $email;
	
	//Check for how many of the default supervises are still supervising the subject
	$query = "select distinct ss.sup_id from default_supervisor as ds, supervises_subject as ss where ds.sub_code = ss.sub_code and ss.sub_code = '$sub_code'";
	//echo $query;
	$result = mysql_query($query);
	
	$remainder_default_supervisors = mysql_num_rows($result);
	//echo "mun rows " . $remainder_default_supervisors;
	
	//Determin if a default supervisor
	$query = "select * from default_supervisor where sub_code = '$sub_code' and sup_id = '$email'";
	$result = mysql_query($query);
	echo $query;
	$is_default_supervisor = false;
	if (mysql_num_rows($result) > 0) {
		$is_default_supervisor = true;	
		echo "true";
	}
	
	if ($is_default_supervisor && ($remainder_default_supervisors <= 1)) {
		//cant delete because is the last supervisor available	
		echo 'not del';
	}
	else {
		$query = "delete from supervises_subject where sup_id = '$email' and sub_code = '$sub_code'";
		$result = mysql_query($query);	
		echo 'deleteing';
	}
	
}

function finalize(){ //to be implemented
	$query = "";
	$result = mysql_query("");
}

function get_allocations(){
	global $email, $array_code, $array_name, $array_day, $array_start, $array_end; 
	
	$query = "select * from supervises_subject as ss, sase_subject as s where ss.sup_id = '$email' and s.sub_code = ss.sub_code order by s.sub_day, s.sub_start, s.sub_end";
	//echo $query;
	$result = mysql_query($query);

	while ($row = mysql_fetch_assoc($result)){
		$array_start[] = $row['sub_start'];
		$array_end[] = $row['sub_end'];
		$array_code[] = $row['sub_code'];
		$array_name[] = $row['sub_name'];
		$array_day[] = $row['sub_day'];
		//echo "doing some thing";
	}
}

$default_combo = "";

if ($_GET['submit']) {
	//Load the combo box, default value is last selected
	$default_combo = $_GET['day'];
	
	$query = 'select * from Exam';
	$result = mysql_query($query);

	$dates = array();
	
	while ($row = mysql_fetch_assoc($result)){
		$tmp = explode(" ", $row['Start_Time']);
		$dates[] = $tmp[0];
	}
	
	$dates = array_unique($dates);
	sort($dates);
	
	$str_dates = "<option value=\"".$default_combo."\">".$default_combo."</option>";

	foreach ($dates as $v) {
		$str_dates =  $str_dates."<option value=\"".$v."\">".$v."</option>";
	}
	
	$query = "select * from Exam where Start_Time > '".$default_combo." 00:00:00' and Start_Time < '".$default_combo." 23:59:59'";
	$result = mysql_query($query);
	
	$sub_array = array();
	
	while ($row = mysql_fetch_assoc($result)){
        $sub_array[] = $row['Sub_Code'];
    }
	
	$str_exams = "";
	
	foreach ($sub_array as $v) {
        $str_exams =  $str_exams."<option value=\"".$v."\">".$v."</option>";
    }
	
}
elseif ($_GET['accept']){
	//Add to database
	$tmp = explode(' ', $_GET['select_exam']);
	$sub_code = strtolower($tmp[0]);
	
	$query = "insert into supervises_exam (Email, Sub_Code) values ('$email', '$sub_code')";
	//echo $query;
	$result = mysql_query($query);
}
else {
	//Just load the first combo box
	$query = 'select * from Exam';
	$result = mysql_query($query);

	$dates = array();
	
	while ($row = mysql_fetch_assoc($result)){
		$tmp = explode(" ", $row['Start_Time']);
		$dates[] = $tmp[0];
	}
	
	$dates = array_unique($dates);
	sort ($dates);
	
	$str_dates = "";

	foreach ($dates as $v) {
		$str_dates =  $str_dates."<option value=\"".$v."\">".$v."</option>";
	}
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Welcome <?php echo $var_supname; ?></title>
<style type="text/css">
body {
	margin-left: 0px;
	margin-top: 0px;
	margin-right: 0px;
	margin-bottom: 0px;
	background-color: #CCC;
}

</style>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>



<body>

<div class="container">
	<div class="headder">
   	  &nbsp;
    </div>
  <div class="menu_bar">
  	<div class="menu_item"><a href="home.php">Home</a></div>
    <div class="menu_item"><a href="home.php?action=search">Add Subject</a></div>
    <div class="menu_item"><a href="home.php?action=change_pwd">Change Password</a></div>
	<div class="menu_item"><a href="home.php?action=logout">Logout</a></div>
  </div>
  
	<div class="sitebody">
    	<div class="left">
        
       	  <p>The following table represents what exams you are currently alloacated into</p>
       	  <table width="92%" border="1" style="margin-left:auto; margin-right:auto;">
            <tr>
            	<td width="80" align="center"> Code</td>
                <td align="center">Subject Name</td>
              	<td width="120" align="center">Date</td>
              	<td width="110" align="center">Start Time</td>
             	<td width="110" align="center">End Time</td>
                <td width="87" align="center">Drop</td>
            </tr>
                
            <?php echo $html_table_body;  ?>  
          </table>
          
          <table width="92%" border="0" style="margin-left:auto; margin-right:auto;">
       			<tr>
                  <td width="59">&nbsp;</td>
                  <td width="627">&nbsp;</td>
                  <td width="111" align="center"><a href="supervisor_view.php?action=finalize">Finalise</a></td>
                  <td width="89" align="center"><a href="add.php">Add</a></td>
                </tr>
          </table>
          
          
          
   	  </div>
	</div>
    <div style="clear:both;"></div>
    <div class="footer">
    
    </div>
</div>


</body>
</html>